//
//  PicCollection.swift
//  WBSwift
//
//  Created by maker on 2016/11/17.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit
import SDWebImage

class PicCollection: UICollectionView {
    
    //设置数据源
    var urls:[URL] = [URL](){
        
        didSet{
            reloadData()
        }
    }
    var indexPath : IndexPath?
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        dataSource = self
        delegate = self
    }

}

extension PicCollection : UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urls.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView .dequeueReusableCell(withReuseIdentifier: "picCell", for: indexPath) as! PicCollectionCell
        cell.url = urls[indexPath.row]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let picDict = [browerIndex:indexPath,browerImageUrls:urls] as [String : Any]
        NotificationCenter.default.post(name: Notification.Name(rawValue: ShowBrower), object: self, userInfo: picDict)
        self.indexPath = indexPath
    }
}

//MARK:==========动画的三个代理==========
extension PicCollection : BrowerAnimatorDelegate{

    func startFrame(_ indexPath: IndexPath) -> CGRect {
        let cell = self.cellForItem(at: indexPath)!
        
        let startFrame = self.convert(cell.frame, to: UIApplication.shared.keyWindow!)
        return startFrame
    }
    
    func endFrame() -> CGRect
    {
        // 1.获取该位置的image对象
        let picURL = urls[self.indexPath!.item]
        let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: picURL.absoluteString)
        
        // 2.计算结束后的frame
        let w = UIScreen.main.bounds.width
        let h = w / (image?.size.width)! * (image?.size.height)!
        var y : CGFloat = 0
        if h > UIScreen.main.bounds.height {
            y = 0
        } else {
            y = (UIScreen.main.bounds.height - h) * 0.5
        }
        
        return CGRect(x: 0, y: y, width: w, height: h)
    }
    func getImageView() -> UIImageView
    {
        let iv = UIImageView()
        let picURL = urls[self.indexPath!.item]
        let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: picURL.absoluteString)
        iv.image = image
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        
        return iv
        
    }
}

class PicCollectionCell: UICollectionViewCell {

    var url : URL? {
        didSet{
            guard let url = url else {
                return
            }
            icon.sd_setImage(with: url, placeholderImage: UIImage(named: "empty_picture") )
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    @IBOutlet weak var icon: UIImageView!
}
















